package week2nd;

import week1st.*;

public class LinkedStack<T> implements StackADT<T> {
    private int count;
    private LinearNode<T> top;
    public LinkedStack(){
        count = 0;
        top = null;
    }

    @Override
    public void push(T element) {
        LinearNode<T> temp = new LinearNode<T>(element);

        temp.setNext(top);
        top = temp;
        count ++;
    }

    @Override
    public T pop() throws EmptyCollectionException {

        if (isEmpty()) {
            throw new EmptyCollectionException("Stack");
        }

        T result = top.getElement();
        top = top.getNext();
        count--;

        return result;
    }

    @Override
    public T peek() {

        if (isEmpty()) {
            throw new EmptyCollectionException("Stack");
        }
        T result = top.getElement();

        return result;
    }

    @Override
    public boolean isEmpty() {

        boolean b ;
        if (size() == 0){
            b = true;
        }
        else
        {
            b = false;
        }
        return b;
    }

    @Override
    public int size() {

        return count;
    }
    @Override
    public String toString(){
        LinearNode node = new LinearNode();
        node = top;
        String str = "";
        while (node != null) {
            str += (node.getElement() + " ");
            node = node.getNext();
        }

        return str;
    }
}
